class UsersController < ApplicationController
  
  layout "management"
  # GET /users
  # GET /users.xml
  def index
    @userInfos = UserInfo.find(:all)
    @users = User.paginate :page => params[:page], :per_page => 6, :order => "nickname"
    respond_to do |format|
      format.html { render :template=> render_to('index')}  #"management/users/index"
      format.xml  { render :xml => @users }
    end
  end
    # GET /users/1
  # GET /users/1.xml
  def show
    @user = User.find(params[:id])
    respond_to do |format|
      format.html { render :template=> render_to('show')}# show.html.erb
      format.xml  { render :xml => @user }
    end
  end

  # GET /users/new
  # GET /users/new.xml
  def new
    @user = User.new
    @roles = Role.find(:all)
    #@dCitys = DCity.find(:all)
    respond_to do |format|
      format.html { render :template=> render_to('new')}# new.html.erb
      format.xml  { render :xml => @user }
    end
  end
  def register
    @user = User.new
#    @roles = Role.find(:all)
    #@dCitys = DCity.find(:all)
    respond_to do |format|
      format.html { render :template=> render_to('register')}# new.html.erb
      format.xml  { render :xml => @user }
    end
  end
  

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
    @user_info = UserInfo.find(:first,:conditions=>["user_id=?",@user.id]) 
    respond_to do |format|
      format.html { render :template=> render_to('edit')}  #"management/users/index"
      format.xml  { render :xml => @users }
    end
  end

  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])
    #@dCitys = Role.find(:all)
    @user_info = UserInfo.new(params[:d_city])
    @user.user_info = @user_info
    
    respond_to do |format|
      if @user.save 
        flash[:notice] = '用户创建成功.'
        format.html { redirect_to(@user) }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        @roles = Role.find(:all)
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /users/1
  # PUT /users/1.xml
  def update
    @user = User.find(params[:id])
    @user_info = @user.user_info
#    roleIDs = params[:roles]
#    @user.roles.clear
#    roleIDs.each do |roleID|
#      @user.roles << Role.find(roleID)
#    end
    respond_to do |format|
      if @user.update_attributes(params[:user])&&@user_info.update_attributes(params[:user_info])
        flash[:notice] = '用户更新成功'
        format.html { redirect_to(:action=>"index") }
        format.xml  { head :ok }
      else
#        @rolesAll = Role.find(:all)
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  
# GET /users/1
  # GET /users/1.xml
  def detail
    begin
      @user = User.find(params[:id])
      @user_detail = @user.user_detail || UserDetail.new(:user_id =>params[:id])
#      flash[:detail_need] = @user.id
    #puts flash[:detail_need]
    rescue ActiveRecord::RecordNotFound
#      @user_detail = UserDetail.new(:user_id =>params[:id])
      flash[:notice] = '用户没有找到'    
    end  
    respond_to do |format|
      format.html { render :template=> render_to('detail')}  #"management/users/index"
      format.xml  { render :xml => @users }
    end
  end
  
  def detail_update
    @user = User.find(params[:id])
    @user_detail = @user.user_detail || UserDetail.new(:user_id =>params[:id])
    respond_to do |format|
       if (@user_detail.update_attributes(params[:user_detail]) &&
          @user_detail.update_attributes(
                                        :sex_id => params[:dsex],
                                        :marriage_id => params[:dmarriag],
                                        :qq_secrecy_id => params[:d_secrecy_type1],
                                        :msn_secrecy_id => params[:d_secrecy_type2]
                                        )
         )
        flash[:notice] = '用户详细信息更新成功.'
        format.html { redirect_to( :action=>"index") }
        format.xml  { head :ok }
      else
        format.html { render :action => "detail" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end


  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    user_detail=UserDetail.find(:all,:conditions=>["user_id=?",@user.id])
    user_info=UserInfo.find(:all,:conditions=>["user_id=?",@user.id])
    user_detail[0].destroy if user_detail.length==1
    user_info[0].destroy if user_info.length == 1
    @user.destroy

    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end
end
